angular
.module('frontEnd', ['ui.router',
	'ngSanitize'],function($httpProvider) {
	// Use x-www-form-urlencoded Content-Type
	$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
	$httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
 
	/**
	* The workhorse; converts an object to x-www-form-urlencoded serialization.
	* @param {Object} obj
	* @return {String}
	*/ 
	var param = function(obj) {
	var query = '', name, value, fullSubName, subName, subValue, innerObj, i;
	for(name in obj) {
	  value = obj[name];
		
	  if(value instanceof Array) {
		for(i=0; i<value.length; ++i) {
		  subValue = value[i];
		  fullSubName = name + '[' + i + ']';
		  innerObj = {};
		  innerObj[fullSubName] = subValue;
		  query += param(innerObj) + '&';
		}
	  }
	  else if(value instanceof Object) {
		for(subName in value) {
		  subValue = value[subName];
		  fullSudebName = name + '[' + subName + ']';
		  innerObj = {};
		  innerObj[fullSubName] = subValue;
		  query += param(innerObj) + '&';
		}
	  }
	  else if(value !== undefined && value !== null)
		query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
	}
	return query.length ? query.substr(0, query.length - 1) : query;
	};
 
	// Override $http service's default transformRequest
	$httpProvider.defaults.transformRequest = [function(data) {
		return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
	}];
})
.config(['$urlRouterProvider', '$stateProvider', function($urlRouterProvider, $stateProvider) {
	$urlRouterProvider.otherwise('/');
	$stateProvider
	.state('home', {
		url: '/:listType',
		templateUrl: 'list.html',
		controller: 'listCtrl'
	})
}])
.factory('getInforService', ['$http', function($http) {
	return {
		get: function(url,param) {
			return $http({
				method: 'GET',
				withCredentials: true,
				params: param,
				url: url,
			})
			.success(function(data, status, headers, config) {
				return data;
			})
		},
		operation: function(url, method, data) {
			return $http({
				method: method,
				withCredentials: true,
				url: url,
				data: data
			})
			.success(function(data, status, headers, config) {
				return data;
			})
		}

	}
}])
.factory('publicService', function() {
	return {
		popHide: function(func) {
			Phenix.init(config).show(function() {
				setTimeout(function() {
					Phenix.hide();
					if(func != undefined) {
						setTimeout(function() {
							func();
						},400);
					}
				},3000);
			});
		},
		popupTip: function(determineSuccess, title, func) {
			if(determineSuccess) {
				data = {
					color: "#36b14a",
					face: ":)",
					title: title
				}
			} else {
				data = {
					color: "#b9b9bd",
					face: ":(",
					title: title
				}
			}
			if(determineSuccess == 'unlogin') {
				config = {
					width: 400,
					height: 200,
					template: document.getElementById('tip_Login').innerHTML,
					data: data
				}
				backUrl = window.location.href;
				Phenix.init(config).show(function() {
					$("#closeLogTip").click(function() {
						Phenix.hide();
					});
					$("#loginBtn").click(function() {
						window.location.href = "http://passport.mengma.com?continue=" + backUrl;
					});
				});
			} else {
				config = {
					width: 500,
					height: 300,
					template: document.getElementById('tip_template').innerHTML,
					data: data
				}
				this.popHide(func);
			}
		},
		popupIntergal: function(title,description,func) {
			config = {
				width: 300,
				height: 350,
				template: document.getElementById('integral_template').innerHTML,
				data: {
					color: "#36b14a",
					title: title,
					description: description
				}
			}
			this.popHide(func);
		}
	};
})
.directive('compile', ['$compile', function ($compile) {
	return function(scope, element, attrs) {
		scope.$watch(
		function(scope) {
		  return scope.$eval(attrs.compile);
		},
		function(value) {
		  element.html(value);
		  $compile(element.contents())(scope);
		}
	)};
}])